var cssHide = "hide";

function load() {

	setEvents();
	pagesManager.setPage(Pages.home, false);
	ajax.get({
		url : "http://localhost:8080/book-service/public/book/authen/info",
		success : function(data, xhr) {
			console.log(data);

			$id("login").addClass(cssHide);
			$id("profile").removeClass(cssHide);
			$id("panel").addClass(cssHide);

			var userName = data.getElementsByTagName("userName")[0].childNodes[0].nodeValue;
			var avatar = data.getElementsByTagName("avatar")[0].childNodes[0].nodeValue;

			$id("profileUserName").innerText = userName.substring(0, 5);
			$id("icon-avatar").src = avatar;
			$id("profilePicture").src = avatar;
			var key = "home";
			addActive(key);
			//sessionStorage.userName=userName.substring(0,5);
		},
		failure : function(xhr) {
			console.log("fail");
			
		},
		abort : function(xhr) {
			callback.abort(xhr);
		}
	});
};

function doPleaseLogin() {
	alert("please log in to access");
	$id("panel").removeClass(cssHide);
	$id("btnLogin").addClass("active");
}

function getProductDetail(event) {
	//var model = new ProductDetailModel().createModel();
	//pagesManager.setPage(Pages.productDetail, true,model);
	//alert(event.currentTarget.id);
	ajax.get({
		url : "http://localhost:8080/book-service/public/book/store/detail?",
		param : {
			"bookID" : event.currentTarget.id
		},
		success : function(data, xhr) {
			var title = data.getElementsByTagName("bookname")[0].childNodes[0].nodeValue;
			var author = data.getElementsByTagName("author")[0].childNodes[0].nodeValue;
			var price = data.getElementsByTagName("price")[0].childNodes[0].nodeValue;
			var des = data.getElementsByTagName("description")[0].childNodes[0].nodeValue;

			var bookId = data.getElementsByTagName("Book")[0].getAttribute("bookID");
			var image = data.getElementsByTagName("bookImage")[0].childNodes[0].nodeValue; 
			var book = new BookDetail(bookId, title, author, price, des,image);

			var commentListServer = data.getElementsByTagName("comment");

			var comments = [];

			for (var i = 0; i < commentListServer.length; i++) {
				console.log(commentListServer[i]);
				var avatar = commentListServer[i].getElementsByTagName("userAvatar")[0].childNodes[0].nodeValue;
				var userName = commentListServer[i].getElementsByTagName("userName")[0].childNodes[0].nodeValue;
				var comment = commentListServer[i].getElementsByTagName("content")[0].childNodes[0].nodeValue;
				var createTime = commentListServer[i].getElementsByTagName("createTime")[0].childNodes[0].nodeValue;
				comments[i] = new Comment(avatar, userName, comment, createTime);
			};

			var model = new Product(book, comments);
			pagesManager.setPage(Pages.productDetail, true, model);
			console.log(data);
			// alert(data.getElementsByTagName("content")[0].childNodes[0].nodeValue);
			//data.getElement
		},
		failure : function(xhr) {
			if(xhr.status==404){
				alert("Xin lỗi bạn. Cuốn sách bạn định xem vừa bị xóa hoặc không tìm thấy");
				load();
			}
		},
		abort : function(xhr) {
			callback.abort(xhr);
		}
	});
};

function uploadFiles(event) {
	var formData = new FormData();
	var file = event.currentTarget[0].files[0];
	formData.append("file", file);
	formData.append("backendUrl", "demo2");

	var xhr = new XMLHttpRequest();
	xhr.open('POST', "http://localhost:8080/book-service/user/changeAvatar", true);
	xhr.onload = function(e) {
		alert("oke");
	};

	xhr.send(formData);
	// multipart/form-data
	alert("return");
	return false;
}

function addActive(key) {
	var arr = ["home", "books", "contact"];
	for (var i = 0; i < arr.length; i++) {
		if (arr[i] == key) {
			$id(key).addClass("active");
			continue;
		}
		$id(arr[i]).removeClass("active");

	}
}

// document.querySelector('input[type="file"]').addEventListener('change', function(e) {
// uploadFiles('/server', this.files);
// }, false);
